package master;
import java.util.Comparator;

import common.WorkerObject;

public class SortByLoad implements Comparator<WorkerObject> {
	
	final static String ACS = "ACS";
	final static String DECS = "DECS";

	private String state;

	public SortByLoad(String state) {
		this.state = state;
	}

	public SortByLoad() {

	}

	public int compare(WorkerObject o1, WorkerObject o2) {
		if (state.equals(DECS)) {
			System.out.println("DECS");
			return descendingSort(o1, o2);
		} else if (state.equals(ACS)) {
			System.out.println("ACS");
			return ascendingSort(o1, o2);
		} else {
			System.out.println("Not a valid param.");
			return 0;
		}
	}

	// sort the object in descending way
	private int descendingSort(WorkerObject o1, WorkerObject o2) {
		if (o1.getLoad() < o2.getLoad()) {
			return 1;
		} else if (o1.getLoad() > o2.getLoad()) {
			return -1;
		} else
			return 0;
	}

	// sort the object in ascending way
	private int ascendingSort(WorkerObject o1, WorkerObject o2) {
		if (o1.getLoad() > o2.getLoad()) {
			return 1;
		} else if (o1.getLoad() < o2.getLoad()) {
			return -1;
		} else
			return 0;
	}
}
